package com.fs.image.test;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by wangxunxiao on 16/9/1.
 */
public class RImageUtil {

    public static String IMAGE_PATH = "/Users/fangshuai/Desktop";
    public static File cover = new File("/Users/fangshuai/Desktop/hahahaha.png");
//    public static File font_SimHei = new File("/opt/soft/tomcat/CarActivity/webapps/ROOT/WEB-INF/classes/SimHei.ttf");
//    public static File font_FZLTZHUNHJW = new File("/opt/soft/tomcat/CarActivity/webapps/ROOT/WEB-INF/classes/FZLTZHUNHJW.TTF");
//    public static Font font = null;
//    public static Font fontSmall = null;
//    public static Font fontBig = null;
    private static Logger logger = LoggerFactory.getLogger(RImageUtil.class);

    private static int x_origin = 112;
    private static int x2_origin = 360;
    private static int x3_origin = 336;
    private static int y_small = 538;
    private static int y_big = 540;
    private static int y_origin = 572;

    public static Color color = new Color(7,74,152);

    public static BufferedImage coverImage = null;

//    public static Font initFont(){
//     try {
//         logger.info("start initFont()");
//
////         FileInputStream fi = new java.io.FileInputStream(font_FZLTZHUNHJW);
////         BufferedInputStream fb = new java.io.BufferedInputStream(fi);
////         font = Font.createFont(Font.TRUETYPE_FONT, fb);
////         int fontStyle = Font.PLAIN;
////         fontBig = font.deriveFont(fontStyle, 33);
////         fontSmall = font.deriveFont(fontStyle, 26);
//
//     } catch (Exception e) {
//            logger.error("init() catch Exception", e);
//        }
//        logger.info("end initFont()");
//
//        return font;
//    }

//    public static void main(String[] args){
//        uploadPhoto();
//    }
//    public static void uploadPhoto(){
//
//        logger.info("start uploadPhoto()");
//
//        try {
//            CloseableHttpClient httpclient = HttpClients.createDefault();
//
//            String ACCESS_TOKEN = WeixinUtil.getAccessToken(false);
//            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+ACCESS_TOKEN+"&type=image");
//            httpPost.getEntity();
//            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
//            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//            multipartEntity.addPart("file", new FileBody(new File(RImageUtil.IMAGE_PATH + "/test.jpg")));
//            httpPost.setEntity(multipartEntity.build());
//            HttpResponse response = httpclient.execute(httpPost);
//            HttpEntity entity = response.getEntity();
//            String responseText = EntityUtils.toString(entity, "UTF-8");
//
//            logger.info(responseText);
//
//        } catch (Exception e) {
//            logger.error("getAccessToken() catch Exception", e);
//
//        }
//        logger.info("end uploadPhoto()");
//    }
    public static synchronized boolean createTargetImage(String srcFileName,String targetFileName) throws IOException, FontFormatException {
        logger.info("start createTargetImage() srcFileName:{}  targetFileName:{}",srcFileName,targetFileName);

        BufferedImage srcImage = ImageIO.read(new File(srcFileName));

//        Integer x1 = user.getX1();
//        Integer x2 = user.getX2();
//        Integer y1 = user.getY1();
//        Integer y2 = user.getY2();

        BufferedImage cuttedmage = srcImage.getSubimage(10, 10, 800, 600);

        BufferedImage userImg = new BufferedImage(750, 419, BufferedImage.TYPE_INT_RGB);

        userImg.getGraphics().drawImage(cuttedmage.getScaledInstance(750, 419, Image.SCALE_SMOOTH), 0, 0, null);


        BufferedImage imageSaved = new BufferedImage(750, 1334, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = imageSaved.createGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.drawImage(userImg, null, 0, 0);

        if (coverImage == null){
            logger.info("createTargetImage() read coverImage");
            coverImage = ImageIO.read(cover);
        }
        g2d.drawImage(coverImage, null, 0, 0);


//        if (font == null){
//            font = initFont();
//        }

//        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
////        if (userType == 1){//员工
//            logger.info("createTargetImage() user is employee");
//            g2d.setFont(fontSmall);
//
//            g2d.setColor(color);
//            FontRenderContext frc = g2d.getFontRenderContext();
//
//            String text1 = "我是美利车金融员工";
//            GlyphVector gv1 = fontSmall.createGlyphVector(frc, text1);
//            g2d.drawGlyphVector(gv1, x_origin, y_small);
//
//            String text2 = "我在" + "等你";
//            GlyphVector gv2 = fontSmall.createGlyphVector(frc, text2);
//            g2d.drawGlyphVector(gv2, x_origin, y_origin);
//            g2d.setFont(fontBig);
//
//
//            GlyphVector gv3 = fontBig.createGlyphVector(frc, "这是一个名字字段");
//            g2d.drawGlyphVector(gv3, x2_origin, y_big);
//
//            logger.info("createTargetImage() drawGlyphVector is done");
//
////        } else if (userType == 2 || userType == 3) {//合作伙伴 用户
////            logger.info("createTargetImage() user is not employee");
////            g2d.setFont(fontBig);
////
////            FontRenderContext frc = g2d.getFontRenderContext();
////
////            g2d.setColor(color);
////            String text1 = "我是"+name;
////            GlyphVector gv1 = fontBig.createGlyphVector(frc, text1);
////            g2d.drawGlyphVector(gv1, x_origin, y_big);
////
////            g2d.setFont(fontSmall);
////
////            String text3= "来自"+location;
////            GlyphVector gv3 = fontSmall.createGlyphVector(frc, text3);
////            g2d.drawGlyphVector(gv3, x3_origin, y_small);
////
////
////            String text5="我在美利车金融选购爱车";
////            GlyphVector gv5 = fontSmall.createGlyphVector(frc, text5);
////            g2d.drawGlyphVector(gv5, x_origin, y_origin);
////            logger.info("createTargetImage() drawGlyphVector is done");
////
////        }
//
//        g2d.dispose();

        boolean result = ImageIO.write(imageSaved, "png", new File(targetFileName));
//        FileOutputStream newimage = new FileOutputStream(fileFullPath);
//        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
//        JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
//                /* 压缩质量 */
//        jep.setQuality(0.9f, true);
//        encoder.encode(im, jep);
//               /*近JPEG编码*/
//        newimage.close();
        logger.info("end createTargetImage() result:{}",result);

        return result;
    }

//    public static String downloadSrcPhoto(String mediaId) throws Exception{
//        logger.info("start downloadSrcPhoto()");
//
//        CloseableHttpClient httpclient = HttpClients.createDefault();
//
//        String accessToken = WeixinUtil.getAccessToken(false);
//        HttpGet httpGet = new HttpGet("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+accessToken+"&media_id="+mediaId);
//
//        CloseableHttpResponse response = httpclient.execute(httpGet);
//        InputStream inputStream = response.getEntity().getContent();
//        String srcFilePath = RImageUtil.IMAGE_PATH+"/"+"src_"+mediaId;
//
//        FileOutputStream fos = new FileOutputStream(srcFilePath);
//        byte[] data = new byte[1024];
//        int len = 0;
//        while ((len = inputStream.read(data)) != -1) {
//            fos.write(data, 0, len);
//        }
//
//        fos.close();
//        inputStream.close();
//        logger.info("end downloadSrcPhoto()");
//        return srcFilePath;
//
//    }

}